//
//  MCAccountSettingVc.swift
//  MCBodyBuildingPro
//
//  Created by Renlvshijie on 2023/5/7.
//

import UIKit
import QMUIKit
import SnapKit
import Toast_Swift
import ObjectMapper
import SwiftyJSON

class MCAccountSettingVc: MCBaseVc {
    
        
    var userProfile:MCUserProfileModel!
    
    var bidngweChatLabel:QMUILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        loadSubViews()
    }
    
    func loadSubViews() -> Void {
        
        self.view.backgroundColor = UIColor.qmui_color(withHexString: "#F7F7F9")
        self.navigationItem.title = "账号设置"
        //包裹
        let containerView = UIView.init()
        containerView.backgroundColor = UIColor.white
        view.addSubview(containerView)
        containerView.snp.makeConstraints { make in
            make.left.equalTo(view.snp.left).offset(16)
            make.right.equalTo(view.snp.right).offset(-16)
            make.top.equalTo(view.safeAreaLayoutGuide.snp.top).offset(35)
            make.height.equalTo(64)
        }
        
        //title
        let titleLabel = QMUILabel.init()
        titleLabel.text = "手机号"
        titleLabel.textColor = UIColor.qmui_color(withHexString: "#333333")
        titleLabel.font = UIFont(name: "PingFangSC-Regular", size: 15)
        containerView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.left.equalTo(containerView.snp.left).offset(16)
            make.centerY.equalTo(containerView.snp.centerY).offset(0)
            make.height.equalTo(45)
        }
        
        
        //subTitle
        let subTitleLabel = QMUILabel.init()
        var strPhone = userProfile.phone ?? ""
        if(strPhone.count == 11){
            let sub2 = strPhone.dropFirst(3).dropLast(4)
            strPhone = strPhone.replacingOccurrences(of: sub2,with: "****")
        }
    
        subTitleLabel.text =  strPhone
        subTitleLabel.font = UIFont(name: "PingFangSC-Regular", size: 15)
        subTitleLabel.textColor = UIColor.qmui_color(withHexString: "#333333")
        containerView.addSubview(subTitleLabel)
        subTitleLabel.snp.makeConstraints { make in
            make.right.equalTo(containerView.snp.right).offset(-16)
            make.centerY.equalTo(containerView.snp.centerY).offset(0)
            make.height.equalTo(45)
        }
        
        //包裹
        let containerWeChatView = UIView.init()
        containerWeChatView.backgroundColor = UIColor.white
        view.addSubview(containerWeChatView)
        containerWeChatView.snp.makeConstraints { make in
            make.left.equalTo(view.snp.left).offset(16)
            make.right.equalTo(view.snp.right).offset(-16)
            make.top.equalTo(containerView.snp.bottom).offset(0)
            make.height.equalTo(64)
        }
        
        let wechatTap = UITapGestureRecognizer(target: self, action:#selector(wechatBinding))
        containerWeChatView.addGestureRecognizer(wechatTap)
        
        //title
        let wechatTitleLabel = QMUILabel.init()
        wechatTitleLabel.text = "微信"
        wechatTitleLabel.textColor = UIColor.qmui_color(withHexString: "#333333")
        wechatTitleLabel.font = UIFont(name: "PingFangSC-Regular", size: 15)
        containerWeChatView.addSubview(wechatTitleLabel)
        wechatTitleLabel.snp.makeConstraints { make in
            make.left.equalTo(containerWeChatView.snp.left).offset(16)
            make.centerY.equalTo(containerWeChatView.snp.centerY).offset(0)
            make.height.equalTo(45)
        }
        
        
        let enterRightImageView = UIImageView.init()
        enterRightImageView.image = UIImage(named: "common_enter_image")
        containerWeChatView.addSubview(enterRightImageView)
        enterRightImageView.snp.makeConstraints { make in
            make.right.equalTo(containerWeChatView.snp.right).offset(-16)
            make.centerY.equalTo(containerWeChatView.snp.centerY).offset(0)
            make.width.equalTo(5)
            make.height.equalTo(9)
        }
        
        //subTitle
        let wechatsubTitleLabel = QMUILabel.init()
        wechatsubTitleLabel.text = "未绑定"
        wechatsubTitleLabel.textColor = UIColor.qmui_color(withHexString: "#35DE94")
        wechatsubTitleLabel.font =  UIFont(name: "PingFangSC-Regular", size: 15)
        containerWeChatView.addSubview(wechatsubTitleLabel)
        wechatsubTitleLabel.snp.makeConstraints { make in
            make.right.equalTo(enterRightImageView.snp.left).offset(-15)
            make.centerY.equalTo(containerWeChatView.snp.centerY).offset(0)
            make.height.equalTo(45)
        }
        
        bidngweChatLabel = wechatsubTitleLabel
        
    }
    
    @objc func wechatBinding() -> Void {
        LQShareSDK.login(.wechat) { uid, name, url, sex in
            
            
            
        } failed: { error in
            self.view.makeToast("绑定失败",position: .center)
        }

    }
    
    
    ///绑定微信
    func WxBinding(code:String?) -> Void {
        NetWorkRequest(MCAccountApi.bingWx(authCode: code ?? "")) { ResponseModel in
            if ResponseModel.code == 200 {
                self.bidngweChatLabel.text = "已绑定"
            }else {
                self.view.makeToast("绑定失败",position: .center)
            }
        } failureCallback: { ResponseModel in
            self.view.makeToast("绑定失败",position: .center)
        }
    }
    
    
}
